package com.oocl.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.oocl.entity.Merchants;
import com.oocl.service.MerchantsService;
import com.oocl.util.RequestUtil;
import com.oocl.vo.MerchantsVo;

@CrossOrigin(origins="*")
@Controller
public class MerchantsController {

	@Autowired
   private MerchantsService merchantsService;
	@Value("#{configProperties['checkLoginUrl']}")
   private String Acess2AS;	
	
	@RequestMapping(value="register")
	public String register(@RequestParam(value="icfile",required=false) MultipartFile icfile,
    		@RequestParam(value="porfile",required = false) MultipartFile porfile,Merchants m ){
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("icpicFile", icfile);
		param.put("portraitFile", porfile);
		param.put("merchant",m);
		try {
			merchantsService.addMerchant(param);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/login.jsp";
	}
	
	@RequestMapping(value="updateRegister")
	public String updateMerchantForRegister(@RequestParam(value="icfile",required=false) MultipartFile icfile,
    		@RequestParam(value="porfile",required = false) MultipartFile porfile,Merchants m ){
		
		Map<String,Object> param = new HashMap<String,Object>();
		param.put("icpicFile", icfile);
		param.put("portraitFile", porfile);
		param.put("merchant",m);
		
		try {
			merchantsService.updateMerchantForRegister(param);
		} catch (Exception e) {
			return "forward:/error.jsp";
		}
		
		return "redirect:/login.jsp";
	}
	
	@RequestMapping(value="login")
	public String login(Merchants m , HttpServletRequest request,Model model){
		MerchantsVo mvo = merchantsService.loginCheck(m);
		System.out.println(m.getMname());
		System.out.println(m.getMpwd());
		try {
		if(mvo!=null){
			ServletContext context = request.getServletContext();
			Map<String,Object> users = (Map<String, Object>) context.getAttribute("users");
			if(users==null){
				users = new HashMap<String,Object>();
			}
			users.put(mvo.getIcnum(), mvo.getStatus1());
			String status2 = mvo.getStatus2();
			System.out.println(status2);
			if("2".equals(status2)){
				request.getSession().setAttribute("status1", mvo.getStatus1());
				request.getSession().setAttribute("name", mvo.getMname());
				request.getSession().setAttribute("mid", mvo.getMid());
				request.getSession().setAttribute("icnum", mvo.getIcnum());
				context.setAttribute("users",users);
				return "redirect:/jsp/foodslist.jsp";
			}else{
				// 对接AS
				System.out.println(Acess2AS);
				String uri = Acess2AS;
				String params = "&icnum="+mvo.getIcnum();
				Map<String,String> info = (Map) RequestUtil.request(uri, "POST", params, Map.class);
				System.out.println(info);
				String status = info.get("status");
				if("2".equals(status)){
					Merchants merchant = new Merchants();
					merchant.setMid(mvo.getMid());
					merchant.setStatus2("2");
					merchantsService.updateMerchant(merchant);
					request.getSession().setAttribute("status1", mvo.getStatus1());
					request.getSession().setAttribute("name", mvo.getMname());
					request.getSession().setAttribute("mid", mvo.getMid());
					request.getSession().setAttribute("icnum", mvo.getIcnum());
					context.setAttribute("users",users);
					return "redirect:/jsp/foodslist.jsp";
				}else{
					model.addAttribute("info", info);
					mvo.setMpwd("");
					model.addAttribute("m", mvo);
					return "forward:/status.jsp";
				}
			}
		}else{
			return "redirect:/login.jsp";
		}	
		} catch (Exception e) {
			e.printStackTrace();
			return "redirect:/login.jsp";
		}
	}
	
	@ResponseBody
	@RequestMapping("updateMerchantsFromA")
	public String updateMerchantFromA(Merchants m ,HttpServletResponse response,HttpSession ses){
		Map<String ,String> map = new  HashMap<String,String>();
		ServletContext context = ses.getServletContext();
		Map<String,Object> users = (Map<String, Object>) context.getAttribute("users");
		if(users!=null){
			String status1 = (String) users.get(m.getIcnum().trim());
			if(status1!=null){
				users.put(m.getIcnum().trim(), m.getStatus1());
				context.setAttribute("users", users);
			}
		}
		try {
			int result  = merchantsService.updateMerchantFromA(m);
			if(result==1){
				map.put("status", "200");
				System.out.println("success");
				return RequestUtil.wrapToJson(map);
			}else{
				map.put("status", "500");
                return RequestUtil.wrapToJson(map);				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.put("status", "200");
		return RequestUtil.wrapToJson(map);	
	}
	
	@ResponseBody
	@RequestMapping("getMerchant")
	public MerchantsVo getMerchant(HttpSession ses){
		String icnum = (String) ses.getAttribute("icnum");
		MerchantsVo mvo = merchantsService.findByIcnum(icnum);
		return mvo;
	}
	
	@ResponseBody
	@RequestMapping("/dobusiness")
	public String dobusiness(Merchants m,HttpSession ses){
		String mid = (String) ses.getAttribute("mid");
		m.setMid(mid);
		try {
			merchantsService.updateMerchant(m);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return m.getOperatStatus();
	}
	
	
}
